#include "SceneManager.h"

#include "SceneLogin.h"
#include "SceneSelectRole.h"
#include "SceneHome.h"
#include "SceneLoading.h"
#include "SceneContainerPack.h"
#include "SceneBattle.h"
#include "SceneTask.h"
#include "LayerManager.h"

CSceneManager::CSceneManager(void) : m_director(cocos2d::Director::getInstance())
{
	for (int32_t i = 0; i != SceneIndexCount; ++i) {
		ChildData& tData = m_ChildDatas[i];
		tData.m_Data = nullptr;
		tData.m_index = (ChildIndex)i;
	}
}


CSceneManager::~CSceneManager(void)
{
}

void CSceneManager::setCurrentScene(CSceneManager::ChildIndex index) {
	switch (index) {
	case SceneIndexLogin:
		runScene<CSceneLogin>(index);
		break;
	case SceneIndexSelectRole:
		runScene<CSceneSelectRole>(index);
		break;
	case SceneIndexHome:
		runScene<CSceneHome>(index);
		break;
	case SceneIndexLoading:
		runScene<CSceneLoading>(index);
		break;
	case SceneIndexContainerPack:
		runScene<CSceneContainerPack>(SceneIndexContainerPack);
		break;
	case SceneIndexBattle:
		pushScene<CSceneBattle>(SceneIndexBattle);
		break;
	case SCeneIndexTask:
		pushScene<CSceneTask>(SCeneIndexTask);
		break;
	}

	cocos2d::Scene* tScene = getRunScene();
	if (nullptr != tScene) {
		for (int32_t i = 0; i != CLayerManager::CommonLayerCount; ++i) {
			auto tNode = CLayerManager::GetInstance()->getCommonLayer(CLayerManager::CommonLayerIndex(i));
			tScene->addChild(tNode, 999);
		}
	}
}

void CSceneManager::popScene() {
	cocos2d::Scene* tRunScene = m_director->getRunningScene();
	ChildData* tRunData = (ChildData*)tRunScene->getUserData();
	this->clearScene(tRunData->m_index);

	m_director->popScene();

	cocos2d::Scene* tScene = getRunScene();
	if (nullptr != tScene) {
		for (int32_t i = 0; i != CLayerManager::CommonLayerCount; ++i) {
			auto tNode = CLayerManager::GetInstance()->getCommonLayer(CLayerManager::CommonLayerIndex(i));
			tScene->addChild(tNode, 999);
		}
	}
}
